package com.slider.sun;

import android.util.Log;

import com.google.gson.Gson;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

/**
 * Created by sun on 2017/5/26.
 */

public class ApiUtils {
    private static final String TAG = "ApiUtils";
    private static HttpUtils httpUtils;
    private static Gson gson;
    private static ApiUtils instance;
    private static final String HOST = "http://admin.sunjj.xyz/api";
//    private static final String HOST = "binsso.api.dyzhx.net";

    public static synchronized ApiUtils getInstance() {
        if (instance == null) {
            instance = new ApiUtils();
        }
        gson = new Gson();
        httpUtils = new HttpUtils();
        httpUtils.configCurrentHttpCacheExpiry(1000);
        return instance;
    }
    public void banner_add(String number, final ApiListener<String> listener){
        String path = "/banner/add";
        RequestParams params=new RequestParams();
        params.addBodyParameter("number",number);
        httpUtils.send(HttpRequest.HttpMethod.POST, HOST + path, params,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e(TAG, responseInfo.result);
                CommonBean result=gson.fromJson(responseInfo.result,CommonBean.class);
                if(result.getCode()==1){
                    listener.onSuccess(result.getData());
                }else {
                    listener.onFailed(result.getMsg());
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {
                Log.e(TAG, s);
                listener.onFailed(s);
            }
        });
    }
    public void banner_select(String number, final ApiListener<BannerRet.DataBean> listener){
        String path = "/banner/select";
        RequestParams params=new RequestParams();
        params.addBodyParameter("number",number);
        httpUtils.send(HttpRequest.HttpMethod.POST, HOST + path, params,new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                Log.e(TAG, responseInfo.result);
                BannerRet result=gson.fromJson(responseInfo.result,BannerRet.class);
                if(result.getCode()==1){
                    listener.onSuccess(result.getData());
                }else {
                    listener.onFailed(result.getMsg());
                }
            }
            @Override
            public void onFailure(HttpException e, String s) {
                Log.e(TAG, s);
                listener.onFailed(s);
            }
        });
    }
}
